<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

// use Illuminate\Http\Request;
// use App\Models\Authorization;
// use App\Transformers\AuthorizationTransformer;

class PmController extends BaseController
{
    /**
     * 获取指定服务商指定媒资的产品包
     * @get("/{spid}/pm/pmtag/{vassetiid}/")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": [error失败|success成功], message="返回信息|null",content": "null|返回数据"})
     */
    public function Pmtags(Request $request, $spid, $termid, $vassetid)
    {
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        if (empty($spid)) {
            return response()->json(['status' => '404', 'data' => '服务商不能为空!']);
        }
        if (empty($vassetid)) {
            return response()->json(['status' => '404', 'data' => '媒资不能为空!']);
        }
        $pmtags = DB::table('pmtag_vassetis as a')
            ->leftJoin('pmtags as b', 'a.pmtag_id', '=', 'b.pmtag_id')
            ->leftJoin('prices as c', 'a.pmtag_id', '=', 'c.pmtag_id')
            ->where('a.vasseti_id', $vassetid)
            ->select('b.*', 'c.prices', 'c.startdate', 'c.enddate', 'c.type', 'c.metering_period_id')
            ->get();
        if (!$pmtags->first()) {
            return response()->json(['status' => '404', 'data' => '没有获取到产品包数据!']);
        }
        $datas = [];
        $data  = [];
        foreach ($pmtags->unique('pmtag_id') as $value) {
            $data['product_id']         = $value->pmtag_id;
            $data['product_name']       = $value->name;
            $data['product_title']      = $value->title;
            $data['product_subtitle']   = $value->subtitle;
            $data['product_short_desc'] = $value->short_desc;
            $data['product_type']       = $value->type;
            $data['images']             = $value->images;
            $data['price']              = $value->prices;
            $data['amount']             = $value->metering_period_id;
            $data['unit']               = $value->metering_period_id;
            $data['exp_begin_time']     = $value->startdate;
            $data['exp_end_time']       = $value->enddate;
            $data['favor_price']        = null;
            $datas[]                    = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取当前媒资所属产品包
     * @get("/{spid}/pm/pmtag/{vassetid}/")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": [error失败|success成功], message="返回信息|null",content": "null|返回数据"})
     */
    public function getPmTagByVasseti(Request $request, $spid, $termid, $vassetid)
    {
        // 服务商
        if (empty($spid)) {
            return response()->json(['status' => '404', 'data' => '服务商ID为空!']);
        }
        // 设备
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        // 媒资
        if (empty($vassetid)) {
            return response()->json(['status' => '404', 'data' => '媒资ID为空!']);
        }
        $vasseti = DB::table('vassetis')->where(['vasseti_id' => $vassetid])->first();
        if (empty($vasseti)) {
            return response()->json(['status' => '404', 'data' => '无此媒资!']);
        }
        // 获取当前媒资所属所有的产品包
        $pmtagVassetis = DB::table('pmtag_vassetis')
            ->where(['vasseti_id' => $vassetid])
            ->pluck('pmtag_id')
            ->unique();
        if (empty($pmtagVassetis->first())) {
            return response()->json(['status' => '404', 'data' => '此媒资无所属产品包!']);
        }
        $data = [];
        foreach ($pmtagVassetis as $key => $value) {
            // 获取当前产品包信息
            $pmtag = $this->getPmTag($value);
            if ($pmtag) {
                // $data['pmtag'][] = $pmtag;
                $data[] = $pmtag;
            }
        }
        // $data['prices'] = $this->getVassetiPrice($vassetid);

        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 根据产品包ID获取产品包信息
     */
    protected function getPmTag($pmtagId)
    {
        $data  = [];
        $pmtag = DB::table('pmtags')
            ->where([
                'pmtag_id' => $pmtagId,
            ])
            ->first();
        // 转义产品包相关字段
        if ($pmtag) {
            $data['pmtag_id']         = $pmtag->pmtag_id;
            $data['pmtag_name']       = $pmtag->name;
            $data['pmtag_title']      = $pmtag->title;
            $data['pmtag_subtitle']   = $pmtag->subtitle;
            $data['pmtag_short_desc'] = $pmtag->short_desc;
            $data['pmtag_images']     = $pmtag->images;
            $data['pmtag_status']     = $pmtag->status;
            $data['pmtag_skd_id']     = $pmtag->skd_id;
            $data['pmtag_sdk_id']     = $pmtag->sdk_id;
            $condition                = ['pmtag_id' => $pmtag->pmtag_id];
            $price                    = $this->getPrice($condition);
            // 如果此产品包无定价改如何去结算金额
            if (empty($price)) {
                $data['price'] = (object) [];
            } else {
                $data['price'] = $price;
            }
        }
        return $data;
    }

    /**
     * 根据定价ID获取定价相关信息
     */
    protected function getPrice($condition)
    {
        $data  = [];
        // 根据不同的条件获取定价相关信息
        $price = DB::table('prices')
            ->where($condition)
            ->first();
        // 转义定价相关字段
        if ($price) {
            $data['price_id']                 = $price->price_id;
            $data['price_title']              = $price->title;
            $data['price_subtitle']           = $price->subtitle;
            $data['price_short_desc']         = $price->short_desc;
            $data['price_prices']             = $price->prices;
            $data['price_metering_period_id'] = $price->metering_period_id;
            $data['price_startdate']          = $price->startdate;
            $data['price_enddate']            = $price->enddate;
            $data['price_type']               = $price->type;
            $data['price_status']             = $price->status;
            $data['price_expiry_date_id']     = $price->expiry_date_id;
            $data['price_sdk_price_id']       = $price->sdk_price_id;
        }
        return $data;
    }

    /**
     *  检查当前媒资是否属于单点定价并有单点定价的信息
     */
    protected function getVassetiPrice($vassetid)
    {
        $data = [];
        // 获取当前媒资所有的定价ID
        $priceIds = DB::table('price_vassetis')
            ->where([
                'vasseti_id' => $vassetid,
            ])
            ->pluck('price_id')
            ->unique();
        if (empty($priceIds->first())) {
            return $data;
        }
        foreach ($priceIds as $key => $value) {
            $condition = ['price_id' => $value];
            $price     = $this->getPrice($condition);
            if ($price) {
                $data[] = $price;
            }
        }
        return $data;
    }
}
